package com.cwj.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cwj.common.domain.user;
import com.cwj.common.utils.utils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
public class loginController {
    @Resource
    private ObjectMapper objectMapper;
    @Resource
    private com.cwj.user.dao.userDao userDao;
    @Resource
    private HttpServletRequest request;
    @RequestMapping("/")
    private String login(){
        Map<String, String> map = utils.GetUserByCookie(request.getCookies());

        Integer user =userDao.selectCount(new QueryWrapper<user>().allEq(map));
        return user==null || user!=1 ? "login" : "index"; }
    @RequestMapping("/loginToIndex")
    private String loginToIndex(user user) throws JsonProcessingException {
        Map<String,String> map = objectMapper.readValue(objectMapper.writeValueAsString(user), new TypeReference<Map<String,String>>() {});

        com.cwj.common.domain.user one = userDao.selectOne(new QueryWrapper<user>().allEq(map));
        return one!=null ?"index" : "login";
    }
}
